package cn.wolfcode.luowowo.web.persistence;

import cn.wolfcode.luowowo.article.domain.StrategyPersistenceVo;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.redis.domian.StrategyStatisVO;
import cn.wolfcode.luowowo.redis.service.IStrategyStatisVoRedisService;
import cn.wolfcode.luowowo.redis.util.BeanUtil;
import cn.wolfcode.luowowo.redis.util.RedisKeys;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
/**
 * 将redis的数据定时持久化到数据库
 */
//@Component
public class RedisDataPersistenceJob {
    @Reference
    private IStrategyStatisVoRedisService strategyStatisVoRedisService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    //定时每10秒触发一次定时器
    @Scheduled(cron="0/10 * * * * ?")
    public void doWork(){
        System.out.println("===========redis数据落地开始====================");
        //将redis的攻略vo更新到数据库中.
        String voKey = RedisKeys.STRATEGY_STATIS_VO.getKey("*");
        List<StrategyStatisVO> list = strategyStatisVoRedisService.listStrategyStatisVo(voKey);
        for (StrategyStatisVO detail : list) {
            StrategyPersistenceVo vo = new StrategyPersistenceVo();
            BeanUtil.copyProperties(vo,detail);
            strategyDetailService.StrategyPersistenceVoUpdate(vo);
        }
        System.out.println("===========redis数据落地结束====================");
    }
}
